﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Web_cuiki
{
    public class TheLoaiDAO
    {
        public static List<THE_LOAI> LayDSTheLoai()
        {
            NhaXuatBanDataContext db = new NhaXuatBanDataContext();
            List<THE_LOAI> ds = null;
            var query = from tl in db.THE_LOAIs
                        select tl;
            ds = query.ToList<THE_LOAI>();
            return ds;
        }
        public static int XoaTheoMa(int maKM)
        {
            int kq = -1;
            NhaXuatBanDataContext db = new NhaXuatBanDataContext();
            try
            {
                var query = from hd in db.THE_LOAIs where hd.MaTheLoai == maKM select hd;
                THE_LOAI hoadon = query.First<THE_LOAI>();
                System.Data.Linq.Table<THE_LOAI> table = db.GetTable<THE_LOAI>();
                table.DeleteOnSubmit(hoadon);
                table.Context.SubmitChanges();
                kq = 1;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return kq;
        }
        public static THE_LOAI TimTheLoaiTheoMa(int maTacGia)
        {
            NhaXuatBanDataContext db = new NhaXuatBanDataContext();

            var query = from hd in db.THE_LOAIs
                        where hd.MaTheLoai == maTacGia
                        select hd;

            return query.First<THE_LOAI>();

        }
        public static THE_LOAI TimTheLoaiTheoTen(string ten)
        {
            NhaXuatBanDataContext db = new NhaXuatBanDataContext();

            var query = from hd in db.THE_LOAIs
                        where hd.TenTheLoai == ten
                        select hd;

            return query.First<THE_LOAI>();

        }
        public static int ThemTheLoai(string tenKieu)
        {
            int kq = -1;
            NhaXuatBanDataContext db = new NhaXuatBanDataContext();
            System.Data.Linq.Table<THE_LOAI> table = db.GetTable<THE_LOAI>();
            try
            {
                THE_LOAI km = new THE_LOAI();
                km.TenTheLoai = tenKieu;
                table.InsertOnSubmit(km);
                table.Context.SubmitChanges();
                kq = 1;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return kq;
        }
    }
}